import router from '@system.router';
import LogUtil from '../../common/baseUtil/LogUtil.js';
import BaseParseConfModel from '../../model/baseParseConfImpl/BaseParseConfModel.js';
import thermal from '@ohos.thermal';
import Utils from '../../model/Utils'

let baseParseConfModel = new BaseParseConfModel();
let logUtil = new LogUtil();
let list = [
    {
        "settingIcon": "/res/image/icon_thermal.png",
        "settingType": 0,
        "settingAlias": "thermalLevel",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_thermal.png",
        "settingType": 0,
        "settingAlias": "subscribeTermal",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_thermal.png",
        "settingType": 0,
        "settingAlias": "lastSubscribeTime",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    }
];

export default {
    data: {
        thermalList: [],
        thermalLevelArr: ["COOL 低温状态 0", "NORMAL 正常状态 1", "WARM 温热状态 2", "HOT 明显发热 3", "OVERHEATED 发热严重 4", "WARNING 即将紧急状态 5", "EMERGENCY 紧急状态 6"],
        thermalJsonPath: '/data/app/el1/bundle/public/com.example.powerhap/com.example.entry/assets/power/resources/rawfile/thermalList.json'
    },
    onInit() {
        this.thermalList = baseParseConfModel.getJsonData(this.thermalJsonPath);
        this.thermalList = this.thermalList || list
        logUtil.info('thermalPage get thermalList by json:' + JSON.stringify(this.thermalList))
        for (let key in this.thermalList) {
            var settingAlias = this.thermalList[key].settingAlias
            this.thermalList[key].settingTitle = this.$t('strings.'.concat(settingAlias))
        }
        logUtil.info('thermalPage onInit end:')
        this.subscribeThermal()
        this.setThermalData()
    },

    thermalLevel(item, level) {
        if (typeof level === "number") {
            item.settingValue = (level >= thermal.ThermalLevel.COOL &&
            level <= thermal.ThermalLevel.EMERGENCY) ?
                this.thermalLevelArr[level] : "状态错误"
        } else {
            item.settingValue = level
        }
    },

    lastSubscribeTime(item) {
        if ('lastSubscribeTime' == item.settingAlias) {
            Utils.getCurrentTime((data) => {
                item.settingValue = data;
            });
        }
    },

    subscribeThermal(){
        console.log(`subscribeThermal`);
        thermal.subscribeThermalLevel((level)=>{
            this.thermalLevel(this.thermalList[1], level)
            this.lastSubscribeTime(this.thermalList[2])
        })
    },
    setThermalData() {
        for (let key in this.thermalList) {
            let item = this.thermalList[key]
            if ('thermalLevel' == item.settingAlias) {
                let level = thermal.getThermalLevel()
                this.thermalLevel(item, level)
            }
        }
    },

    pullDown() {
        this.setThermalData();
    },
    thermalBack() {
        router.back();
    },
    onBackPress() {
        router.back();
    },
    onCreate() {
    },
    onReady() {
    },
    onShow() {
        this.setThermalData()
    },
    onHide() {
    },
    onDestroy() {
        console.log(`onDestroy unsubscribeThermalLevel`);
        thermal.unsubscribeThermalLevel();
    }
}
